package cn.pomit.springbootwork.lock.redis.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.pomit.springbootwork.lock.redis.dto.ResultModel;
import cn.pomit.springbootwork.lock.redis.inter.ConsumeService;
import cn.pomit.springbootwork.lock.redis.inter.LockWorker;
import cn.pomit.springbootwork.lock.redis.lock.DistributedLocker;

@Service("consumeService")
public class UserConsumeServiceProxy extends DistributedLocker implements ConsumeService {
	@Autowired
	UserConsumeService userConsumeService;

	public ResultModel goodList(Integer page, Integer size) {
		return userConsumeService.goodList(page, size);
	}

	public ResultModel goodDetail(Integer goodId) {
		return userConsumeService.goodDetail(goodId);
	}

	public ResultModel consume(Integer goodId, Integer num, String userName) {
		LockWorker lockWorker = new LockWorker() {
			@Override
			public ResultModel invoke() throws Exception {
				log.info("开始远程消费：userName：{};goodId:{},num:{}", userName, goodId, num);
				return userConsumeService.consume(goodId, num, userName);
			}
		};

		return tryLock(lockWorker);
	}

}
